<template>
	<view>
		<view class="header">
			<view class="statusBar"></view>
			<view class="navBar">
				<view @click="back">
					<u-icon name="arrow-left" color="#FFFFFF" size="48"></u-icon>
				</view>
				<view class="laction" @click="showPicker">
					{{city}}
					<u-icon name="map" color="#FFFFFF" size="32"></u-icon>
				</view>
				<view class="searchBox">
					<u-icon name="search" color="#FFFFFF" size="32"></u-icon>
					<input type="text" value="" placeholder="健身" placeholder-style="fong-size:28rpx;color:#F2F2F2" />
				</view>
				<view class="scan" @click="scanCode">
					<u-icon name="scan" color="#FFFFFF" size="48"></u-icon>
				</view>
			</view>
		</view>
		<view class="tabBox">
			<view class="tabTop">
				<view class="tabTop-li" v-for="(tabItem,tabx) in topTab" :key='tabx'
					@click="navTo('./sort/sort?id='+tabItem.id+'&lat='+lat +'&lng='+lng +'&citycode='+citycode)">
					<image :src="tabItem.icon" mode=""></image>
					<text>{{tabItem.cate_name}}</text>
				</view>
			</view>
			<view class="tabBot">
				<view class="tabBot-li" v-for="(item,index) in botTab" :key='index'
					@click="navTo('./sort/sort?id='+item.id+'&lat='+lat +'&lng='+lng +'&citycode='+citycode)">
					<image :src="item.icon" mode=""></image>
					<text>{{item.cate_name}}</text>
				</view>
			</view>
		</view>
		<view class="tab-bar">
			<u-tabs ref="uTabs" :list="list" :current="current" @change="tabsChange" bar-width='80' bar-height="8"
				inactive-color='#333333' bg-color='#f2f2f2' :bar-style='barStyle' :active-item-style='itemStyle'
				:is-scroll="false"></u-tabs>
		</view>
		<view class="content" v-show="shopList.length">
			<u-waterfall v-model="shopList" ref="shopWaterfall">
				<template v-slot:left="{leftList}">
					<view class="storeItem" v-for="(item, index) in leftList" :key="index"
						@click="navTo('./shop/shop?id='+item.id)">
						<image :src="item.icon" mode=""></image>
						<view class="title">
							{{item.shop_name}}
						</view>
						<view class="rate">
							<u-icon name="star-fill" color="#FE8C16" size="28"></u-icon>
							<text>{{item.score}}分</text>
						</view>
						<view class="price">
							¥ {{item.average_cost}}/人
						</view>
						<view class="address">
							{{item.city+'市'+item.district+'区'+ item.address}}
						</view>
					</view>
				</template>
				<template v-slot:right="{rightList}">
					<view class="storeItem" v-for="(item, index) in rightList" :key="index"
						@click="navTo('./shop/shop?id='+item.id)">
						<image :src="item.icon" mode=""></image>
						<view class="title">
							{{item.shop_name}}
						</view>
						<view class="rate">
							<u-icon name="star-fill" color="#FE8C16" size="28"></u-icon>
							<text>{{item.score}}分</text>
						</view>
						<view class="price">
							¥ {{item.average_cost}}/人
						</view>
						<view class="address">
							{{item.city+'市'+item.district+'区'+ item.address}}
						</view>
					</view>
				</template>
			</u-waterfall>
		</view>
		<view class="noCont" v-if="shopList.length == 0" style="padding-top: 100rpx;">
			<u-empty text="暂无门店" mode="address"></u-empty>
		</view>
		<!-- 省市级联 -->
		<aui-picker ref="picker" :title="auiPicker.title" :layer="auiPicker.layer" :data="auiPicker.data"
			@callback="pickerCallback"></aui-picker>

	</view>
</template>

<script>
	import myRequest from '@/api/index.js'
	import auiPicker from '@/components/aui-picker/aui-picker.vue';
	export default {
		components: {
			auiPicker,
		},
		data() {
			return {
				auiPicker: {
					title: '区域选择',
					layer: null,
					data: []
				},
				list: [],
				current: 0,
				barStyle: {
					background: 'linear-gradient(180deg, #93B9F9 0%, #2D7AFE 100%)',
				},
				itemStyle: {
					fontWeight: 'bold',
					color: '#333333'
				},
				lat: '', //纬度
				lng: '', //经度
				page: '1',
				city: '', //城市
				citycode: '', // 城市码
				cityList: [],
				keyword: '',
				shopList: [],
				topTab: [], // 分类
				botTab: [], // 分类
			}
		},
		onLoad() {
			let that = this
			// 获取位置信息
			uni.getLocation({
				type: 'gcj02',
				geocode: true,
				success(res) {
					that.lat = res.latitude
					that.lng = res.longitude
					that.city = res.address.city
					that.getMyregion()
				}
			})
			this.getShopType()
			this.getOtgertype()
		},
		onReachBottom() {
			let page = Number(this.page) + 1
			this.page = page.toString()
			this.getShop(this.lat, this.lng, this.citycode)
		},
		methods: {
			// 扫一扫
			scanCode() {
				uni.scanCode({
					success: function(res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
					}
				})
			},
			async getOtgertype() {
				let res = await myRequest.getAmusementtype()
				if (res.status) {
					res.data.forEach(item => {
						let obj = {
							name: item.cate_name
						}
						this.list.push(obj)
					})
				}
			},
			// 获取省市信息
			async getMyregion() {
				let that = this
				let city = that.city.split('市')[0]
				let res = await myRequest.getregion()
				if (res.status) {
					let i = 0
					res.data.forEach((item, k) => {
						if (k != i) {
							this.cityList.push(item)
						}
					})
					// 匹配当前定位的城市代码
					this.cityList.forEach(k => {
						k.citys.forEach(i => {
							if (i.name == city) {
								that.citycode = i.code
							}
						})
					})
					that.getShop(that.lat, that.lng, that.citycode)
				}
			},
			pickerCallback(e) {
				this.page = '1'
				this.$refs.shopWaterfall.clear()
				this.citycode = e.data[1].id
				this.city = e.data[1].name + '市'
				this.getShop(this.lat, this.lng, this.citycode)
			},
			showPicker() {
				// 处理省市数据
				let list = []
				this.cityList.forEach(item => {
					let cityObj = {}
					cityObj.name = item.name
					cityObj.id = item.code
					cityObj.children = []
					item.citys.forEach(k => {
						let childObj = {}
						childObj.name = k.name
						childObj.id = k.code
						cityObj.children.push(childObj)
					})
					list.push(cityObj)
				})
				const _this = this;
				_this.auiPicker.layer = 2;
				_this.auiPicker.data = list
				_this.$refs.picker.open()
			},
			// 门店列表
			async getShop(lat, lng, citycode) {
				let {
					page
				} = this
				let res = await myRequest.getShoplist({
					page: page,
					num: '5',
					lat: lat.toString(),
					lng: lng.toString(),
					city_code: citycode ? citycode : '',
				})
				if (res.status) {
					this.shopList = [...this.shopList, ...res.data]
				}
			},
			// 获取分类
			async getShopType() {
				let that = this
				let res = await myRequest.getCategorytype()
				if (res.status) {
					let len = res.data.length
					this.topTab = res.data.slice(0, 5)
					this.botTab = res.data.slice(5, len)
				}
			},
			navTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			tabsChange(index) {
				this.current = index
			},
			back() {
				uni.navigateBack({
					delta: 1
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.header {
		width: 100%;
		height: 658rpx;
		background: linear-gradient(179deg, #005EFF 0%, #F2F2F7 100%);

		.statusBar {
			width: 100%;
			height: var(--status-bar-height);
			background-color: rgba(53, 126, 253, 1);
			position: fixed;
			top: 0;
			z-index: 100;
		}

		.navBar {
			width: 100%;
			height: 88rpx;
			padding: 0 20rpx;
			display: flex;
			align-items: center;
			position: fixed;
			top: var(--status-bar-height);
			background-color: rgba(53, 126, 253, 1);
			z-index: 100;

			.laction {
				margin-left: 10rpx;
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #FFFFFF;
			}

			.searchBox {
				margin-left: 10rpx;
				width: 466rpx;
				height: 64rpx;
				background: rgba(255, 255, 255, 0.3);
				border-radius: 20px;
				display: flex;
				align-items: center;
				padding-left: 28rpx;

				input {
					font-size: 28rpx;
					color: #FFFFFF;
					text-indent: 10rpx;
				}
			}

			.scan {
				margin-left: auto;
			}
		}
	}

	.tabBox {
		width: 710rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
		margin: -514rpx auto 0;
		padding: 0 20rpx 30rpx;

		.tabTop {
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 2rpx solid #F2F2F2;

			.tabTop-li {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				padding: 30rpx 0 20rpx;

				image {
					width: 120rpx;
					height: 120rpx;
					border-radius: 50%;
				}

				text {
					margin-top: 10rpx;
					font-size: 24rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #333333;
				}
			}
		}

		.tabBot {
			display: flex;
			flex-wrap: wrap;
			align-items: center;

			.tabBot-li {
				margin-top: 40rpx;
				width: 120rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 17.5rpx;

				&:nth-child(5n) {
					margin-right: 0rpx;
				}

				image {
					width: 48rpx;
					height: 48rpx;
				}

				text {
					font-size: 24rpx;
					font-family: Segoe UI;
					font-weight: 400;
					color: #333333;
					margin-top: 10rpx;
				}
			}

		}
	}

	.tab-bar {
		width: 24%;
		margin-top: 20rpx;
	}

	.content {
		padding: 20rpx 15rpx 0;

		.storeItem {
			width: 348rpx;
			height: auto;
			background: #FFFFFF;
			border-radius: 16rpx;
			padding-bottom: 18rpx;
			margin-bottom: 20rpx;
			overflow: hidden;

			image {
				width: 348rpx;
				height: 348rpx;
			}

			.title {
				padding: 0 10rpx;
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #333333;
			}

			.rate {
				margin-top: 8rpx;
				padding-left: 10rpx;
				font-size: 24rpx;
				font-family: Segoe UI;
				font-weight: 400;
				color: #FE8C16;

				text {
					margin-left: 8rpx;
				}
			}

			.price {
				margin-top: 8rpx;
				padding-left: 10rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #333333;
			}

			.address {
				margin-top: 10rpx;
				padding: 0 10rpx;
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #999999;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
			}
		}
	}
</style>
